home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / src / exampleCode / games / IndiZone / cycles / set-speed.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-08-02  |  2.1 KB  |  89 lines

  1.  
  2. #include <sys/types.h>    /* for times */
  3. #include <sys/times.h>    /* for times */
  4. #include <sys/param.h>    /* for times */
  5. #include "cycles.h"
  6.  
  7. #ifndef MIN
  8. #define MIN(x, y)  ((x)<(y)?(x):(y))
  9. #endif
  10.  
  11. extern float speed_fac;
  12.  
  13. /*
  14.  * attempt to set a scaling factor so that all
  15.  * cycles move at the same speed, even on way faster machines
  16.  */
  17. void set_speed_fac(int starting) {
  18.     static int count;
  19.     static clock_t init_ticks, old_ticks[5], prev_ticks;
  20.     clock_t now;
  21.     int i, real_count;
  22.     float last5_av, all_av, this_time, predict_time;
  23.     struct tms t;
  24.  
  25.     /*
  26.      * REAL_SPEED is the dist a cycle should move in one second
  27.      * frame_ticks/HZ is the seconds it took to draw the last frame
  28.      * so speed_fac = (frame_ticks/HZ)*REAL_SPEED
  29.      */
  30.  
  31.     now = times(&t);
  32.  
  33.     if (starting) {
  34.     count = 1;
  35.     init_ticks = now;
  36.     prev_ticks = now;
  37.  
  38.     old_ticks[0] = 20;    /* no idea so guess these... */
  39.     speed_fac = 1.0;
  40.     return;
  41.     }
  42.  
  43.     count++;
  44.     real_count = MIN(5, count);
  45.  
  46.     /* shuffle down last times */
  47.     if (count >= 5)
  48.     for (i = 0; i < 5-1; i++) old_ticks[i] = old_ticks[i+1];
  49.     old_ticks[real_count-1] = now - prev_ticks;
  50.  
  51.     /* find average of last 5 loops, and all prev loops */
  52.     last5_av = 0.0;
  53.     for (i = 0; i < real_count; i++) last5_av += (float)old_ticks[i];
  54.  
  55.     /* convert all to seconds */
  56.     this_time = (float)(now - prev_ticks)/(float)(HZ);
  57.     last5_av /= (float)(HZ*real_count);
  58.     all_av = (float)(now - init_ticks)/(float)(HZ*(count - 1));
  59.  
  60.     /*
  61.      * some linear combination formula to hopefully get over
  62.      * local and network hiccups
  63.      */
  64.     predict_time = 0.5*this_time + 0.2*last5_av + 0.1*all_av;
  65.  
  66.     /* set the speed_fac for this frame based upon the last time taken */
  67.     speed_fac = REAL_SPEED*predict_time;
  68.  
  69. #ifdef DEBUG
  70. printf("times: %d, last_ticks %d, speed_fac %g (real_count %d)\n",
  71.  count, now - prev_ticks, speed_fac, real_count);
  72. #endif
  73.  
  74.     prev_ticks = now;
  75.  
  76. #if 0
  77. /* weird but cute */ {
  78. int i;
  79. extern CYCLE *good, bike[CYCLES];
  80.   for (i = 0; i < CYCLES; i++)
  81.      if (i*40 == count%200 && i != good->id) {
  82.     bike[i].fall++;
  83.     bike[i].falling++;
  84.     bike[i].who_we_hit = -1;
  85.     }
  86. }
  87. #endif
  88. }
  89.